/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import inquiry.ItemInquiry;
import java.util.List;
import javax.persistence.Query;
import javax.swing.table.DefaultTableModel;
import myJPA.Model;

/**
 *
 * @author nacerkhalil
 */
public class RespondToTitleInquiry extends javax.swing.JPanel {

    /**
     * Creates new form RespondToTitleInquiry
     */
    public RespondToTitleInquiry() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        refreshButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Customer", "Item", "Message"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable1MousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jButton1.setText("Send response");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(68, 68, 68)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jButton1)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(jScrollPane2)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, refreshButton)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE)))
                .addContainerGap(112, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 233, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(refreshButton)
                .add(18, 18, 18)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton1)
                .addContainerGap(34, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        try
        {
            Query query = Model.getEntityManager().createNamedQuery("getInquiries");
            list = (List<ItemInquiry>) query.getResultList();
            for(ItemInquiry item: list)
            {
                if(item.getResponse().length() > 0)
                    list.remove(item);
            }
            DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
            for(int i = 0; i < model.getRowCount(); i++)
                model.removeRow(i);
            for(ItemInquiry inquiry: list)
            {
                String[] row = new String[3];
                row[0] = inquiry.getCustomer().getFullName();
                row[1] = inquiry.getItem().getTitle();
                row[2] = inquiry.getMessage();
                model.addRow(row);
            }
            
        }
        catch(Exception ex)
        {
            
        }
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
        index = jTable1.getSelectedRow();
        
    }//GEN-LAST:event_jTable1MousePressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        list.get(index).setResponse(jTextArea1.getText());
        list.get(index).update();
        list.remove(index);
        
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        for(int i = 0; i < model.getRowCount(); i++)
            model.removeRow(i);
        for(ItemInquiry inquiry: list)
        {
            String[] row = new String[3];
            row[0] = inquiry.getCustomer().getFullName();
            row[1] = inquiry.getItem().getTitle();
            row[2] = inquiry.getMessage();
            model.addRow(row);
        }
        jTextArea1.setText("");
            
    }//GEN-LAST:event_jButton1ActionPerformed
    private List<ItemInquiry> list;
    private int index;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton refreshButton;
    // End of variables declaration//GEN-END:variables
}
